using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

namespace ApiProject.Api.Contriller
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class AdminController : ControllerBase
    {
        private IRepository<Admins> _adminsRespository;
        private IRepository<AdminInfo> _adminInfoRepository;

        private readonly IConfiguration _configuration;
        private readonly IWebHostEnvironment _hostingEnvironment;
        private Token _tokenParameter;


        public AdminController(IConfiguration configuration, IRepository<AdminInfo> adminInfoRepository, IRepository<Admins> adminsRespository, IWebHostEnvironment hostingEnvironment)
        {
            _adminsRespository = adminsRespository;
            _configuration = configuration;
            _adminInfoRepository = adminInfoRepository;
            _tokenParameter =
              _configuration.GetSection("TokenParameter").Get<Token>();
            _hostingEnvironment = hostingEnvironment;
        }

        //后台管理系统 admin管理 分页显示admin列表
        [HttpGet]
        public dynamic Get([FromQuery] Pages page)
        {
            var pageIndex = page.pageIndex;
            var pageSize = page.pageSize;

            var users = _adminsRespository.Table.Include(p => p.AdminInfo).Where(o => o.IsDelete == false && o.Id != 1).ToList();
            var u = users.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = new { Data = u, pager = new { pageIndex, pageSize, rowsTotal = users.Count() } },
                Msg = "获取成功"
            });
        }



        //伪删除管理员
        [HttpPost, Route("delete/{id}")]
        public dynamic IsDelete(int id)
        {
            var user = _adminsRespository.GetById(id);

            if (user != null)
            {
                user.IsDelete = true;
                user.UpdatedTime = DateTime.Now;

                _adminsRespository.Update(user);
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = user,
                    Msg = string.Format("成功在表中删除Id为{0}的管理员", id)
                });
            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "删除失败" });
            }
        }

        //禁用管理员
        [HttpPost, Route("isenable/{id}")]
        public dynamic IsEnable(int id, NewAdmin model)
        {
            var user = _adminsRespository.GetById(id);

            var num = model.isEnable;

            if (user != null)
            {
                if (num == false)
                {
                    user.IsEnable = false;
                    user.UpdatedTime = DateTime.Now;
                    _adminsRespository.Update(user);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = user,
                        Msg = string.Format("已禁用Id为{0}的管理员", id)
                    });
                }
                else
                {
                    user.IsEnable = true;
                    user.UpdatedTime = DateTime.Now;
                    _adminsRespository.Update(user);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = user,
                        Msg = string.Format("已启用Id为{0}的管理员", id)
                    });
                }

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "禁用失败" });
            }
        }

        //修改管理员
        [HttpPut, Route("edituser/{id}")]
        public dynamic EditUser(int id, NewAdmin newUser)
        {

            var user = _adminsRespository.GetById(id);

            if (user != null)
            {

                user.Username = newUser.Username;
                user.Password = newUser.Password;
                user.Remarks = newUser.Remarks;
                user.UpdatedTime = DateTime.Now;

                _adminsRespository.Update(user);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = user,
                    Msg = "修改管理员成功"
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = user,
                    Msg = "修改管理员失败"
                });
            }
        }

        //添加管理员
        [HttpPost]
        public dynamic Post(NewAdmin model)
        {
            var username = model.Username.Trim();
            var password = model.Password.Trim();

            // 判断传入的username是否为空
            if (string.IsNullOrEmpty(username))
            {
                var temp =
                    new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "管理员名不能为空，请输入用户名"
                    };
                return JsonHelper.Serialize(temp);
            }
            else
            {
                var users = _adminsRespository.Table.Where(x => x.Username == username).FirstOrDefault();
                //判断是否存在该用户
                if (users != null)
                {
                    if (users.IsDelete == true)
                    {
                        var user =
                            new Admins
                            {
                                Username = username,
                                Password = password
                            };
                        _adminsRespository.Insert(user);
                        _adminsRespository.Table.Where(x => x.Username == username).FirstOrDefault();
                        var AdminInfo = new AdminInfo
                        {
                            AdminId = user.Id,
                            RealName = model.RealName
                        };

                        _adminInfoRepository.Insert(AdminInfo);

                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 200,
                                Data = user,
                                Msg = "创建管理员成功"
                            });
                    }
                    else
                    {
                        return JsonHelper.Serialize(new
                        {
                            Code = 1000,
                            Data = "",
                            Msg = "管理员已存在，请重新输入用户名"
                        });
                    }
                }
                else
                {
                    var user =
                        new Admins
                        {
                            Username = username,
                            Password = password
                        };
                    _adminsRespository.Insert(user);
                    _adminsRespository.Table.Where(x => x.Username == username).FirstOrDefault();
                    var AdminInfo = new AdminInfo
                    {
                        AdminId = user.Id,
                        RealName = model.RealName
                    };

                    _adminInfoRepository.Insert(AdminInfo);

                    return JsonHelper
                        .Serialize(new
                        {
                            Code = 200,
                            Data = user,
                            Msg = "创建管理员成功"
                        });
                }
            }
        }



        //显示个人信息
        //Get /admin/getAdminInformation/{id}
        [HttpGet, Route("getAdminInformation/{id}")]
        public dynamic Get(int id)
        {
            var adminInfo = _adminInfoRepository.Table.Where(x => x.AdminId == id && x.IsDelete == false).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = adminInfo,
                Msg = "获取成功"
            });
        }


        //修改个人信息
        //put /admin/addAdminInfo/{id}
        [HttpPost("{id}")]
        public dynamic Putace(int id, NewAdminInfo model)
        {
            var realName = model.RealName.Trim();
            var sex = model.Sex.Trim();
            var birthday = model.Birthday;
            var address = model.Address.Trim();
            var selfIntroduction = model.SelfIntroduction.Trim();

            var admingetInfo = _adminInfoRepository.Table.Where(x => x.AdminId == id).FirstOrDefault();

            if (admingetInfo == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "请确认后重试"
                });
            }
            else
            {

                admingetInfo.RealName = realName;
                admingetInfo.Sex = sex;
                admingetInfo.Birthday = birthday;
                admingetInfo.Address = address;
                admingetInfo.SelfIntroduction = selfIntroduction;

                _adminInfoRepository.Update(admingetInfo);
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = admingetInfo,
                    Msg = "修改个人信息成功"
                });
            }






        }




    }
}